package remoteRpc

import (
	baseLog "gitee.com/zaiqiang231/go-base-app/base_app/log"
	"gitee.com/zaiqiang231/go-base-app/example/rpc/proto"
	"google.golang.org/grpc"
	"sync"
)

var (
	remoteTestRpc       *RemoteTestRpc
	onceSessionRpcCache sync.Once
)

func GetTestRpc() *RemoteTestRpc {
	if remoteTestRpc == nil {
		baseLog.Errorf("remoteTestRpc instance is %v", remoteTestRpc)
	}
	return remoteTestRpc
}

type RemoteTestRpc struct {
	TestRpcServiceClient proto.TestRpcServiceClient
}

func InitTestRpc(rpcAddr string) (result *RemoteTestRpc, err error) {
	onceSessionRpcCache.Do(func() {
		conn, tmperr := grpc.Dial(rpcAddr, grpc.WithInsecure())
		if tmperr != nil {
			err = tmperr
			return
		}
		remoteTestRpc = &RemoteTestRpc{
			TestRpcServiceClient: proto.NewTestRpcServiceClient(conn),
		}
		result = remoteTestRpc
	})
	return
}
